<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
        <title>增加广告信息</title>
        
        <link rel="stylesheet" type="text/css" href="../css/cm-default.css"  />
        <link rel="stylesheet" type="text/css" href="../ligerUI/skins/Aqua/css/ligerui-all.css" />
        <link rel="stylesheet" type="text/css" href="../ligerUI/skins/ligerui-icons.css" />
        <script src="../js/jquery-min.js" type="text/javascript"></script>
        <script src="../ligerUI/js/ligerui.min.js" type="text/javascript"></script>
        <script src="../js/common.js" type="text/javascript"></script>
        <script src="../js/json2.js" type="text/javascript"></script>
        <script type="text/javascript" src="../js/md5.js"></script>
         <script type="text/javascript" src="../js/ajaxfileupload.js"></script>
        <script src="../js/validate/jquery.validate.js" type="text/javascript"></script>
        <script src="../js/validate/jquery.metadata.js" type="text/javascript"></script>
        <script src="../js/validate/messages_zh.js" type="text/javascript"></script>
        <script type="text/javascript">
             var opt,c_code,shop_no; 
            $(function(){
                var query = getQueryString(window.location.toString());
                opt = query.o;
                if (opt) {
                    c_code = query.code;
                    shop_no = query.shop_no;
                    if (opt === 'm') {
                        var param = {advertise_no:c_code};
                        var data = JSON.stringify(reqObj('q','wifi.select.advertise_basic_info.select',JSON.stringify(param)));
                        ajaxSubmit("/cmwifi/rbac/cm.do?m=c", data, function(data) {
                            bindVal(data['data']);
                        }, null, false);
                        
                        ajaxSubmit("/cmwifi/rbac/cmUser.do?method=getUserInfo", null, function(data) {
                            $('#WriteUserCodeS').val(data['uid']);
                            $("#WriteDateS").val(data['date']);
                        }, null, false);
                    }else{
                    	  $('#shop_no').val(shop_no);
                          $('#shop_nameS').val(paramRender(null,'shop_list',shop_no));
                        ajaxSubmit("/cmwifi/rbac/cmUser.do?method=getUserInfo", null, function(data) {
                            $('#createUserS').val(data['uname']);
                            $('#createUserCodeS').val(data['uid']);
                            $("#createDateS").val(data['date']);
                        }, null, false);
                    }
                }
                
                //是否有效
                $("#advertise_useableTxtS").ligerComboBox({
                    autocomplete: true,
                    valueField:'value',
                    textField:'name',
                    data:getData('rbac.status'),
                    onSelected:function(v,t) {
                        $("#advertise_useable").val(v);
                     }
               });
                
                $("#wifi_ssidS").ligerComboBox({
                    autocomplete: true,
                    valueField:'value',
                    textField:'name',
                    data:getData('wifi_list',{shop_no:shop_no},true),
                    onSelected:function(v,t) {
                        $("#wifi_no").val(v);
                     }
               });
                
                $("#advertise_levelTxtS").ligerComboBox({
                    autocomplete: true,
                    valueField:'value',
                    textField:'name',
                    data:getData('ad_level'),
                    onSelected:function(v,t) {
                        $("#advertise_level").val(v);
                     }
               });
                $("#pageloading").hide();
                });
           //保存处理中值check处理
           function validate() {
               var valid = true;
               $('*[id$="S"],*[id$="S_txt"]').each(function(index, element) {
                   if (!$(this).rules()) return;
                   if (!$(element).valid()) {
                       valid = false;
                       return;
                   }
               });
               if (!valid) return false;
               return true;
           }
           //重置处理
           function resetArea() {
               toReset('#a_center input:not(.readonly),select,textarea');
           }
           // 查询时数据值绑定  应该可以抽象为公共的功能
           function bindVal(data) {
               if (data.Rows && data.Rows.length === 1) {
                   data = data.Rows[0];
                   $('#advertise_no').val(data.advertise_no);
                   $('#advertise_nameS').val(data.advertise_name);
                   $('#advertise_dateS').val(data.advertise_date);
                   $('#advertise_levelTxtS').val(paramRender(null,'ad_level',data.advertise_level));
                   $('#advertise_level').val(data.advertise_level);
                   $('#advertise_useableTxtS').val(paramRender(null,'rbac.status',data.advertise_useable));
                   $('#advertise_useable').val(data.advertise_useable);
                   $('#shop_no').val(data.shop_no);
                   $('#shop_nameS').val(data.shop_name);
                   $('#wifi_no').val(data.wifi_no);
                   $('#wifi_ssidS').val(data.wifi_ssid);
                   $('#createUserS').val(paramRender(null,'user',data.createuser));
                   $('#createUserCodeS').val(data.createuser);
                   $('#createDateS').val(data.createdate);
               }
           }
           // 保存业务处理
           function toAdd() {
              if (!validate())
                  return;
               var param = {
            		   advertise_no:$('#advertise_no').val(),
            		   advertise_name:$('#advertise_nameS').val(),
            		   advertise_date:$('#advertise_dateS').val(),
            		   advertise_level:$('#advertise_level').val(),
            		   advertise_useable:$('#advertise_useable').val(),
                       shop_no:$('#shop_no').val(),
                       shop_name:$('#shop_nameS').val(),
                       wifi_no:$('#wifi_no').val(),
                       wifi_ssid:$('#wifi_ssidS').val(),
               };
               var en = 'wifi.add.advertise_basic_info.insert', optType = 'a';
               if (opt === 'm') {
                   
                   //修改人
                   param.writeuser=$('#WriteUserCodeS').val();
                   //修改日期
                   param.writedate=$('#WriteDateS').val();
                   // 进行修改数据库处理
                   en = 'wifi.update.advertise_basic_info.update';
                   // 操作类型为 update
                   optType = 'u';
               } else {
                   // 新增时 数据处理
                   param.createuser = $("#createUserCodeS").val();
                   param.createdate = $("#createDateS").val();
               }
               var data = JSON.stringify(reqObj(optType,en,JSON.stringify(param)));
               ajaxSubmit("/cmwifi/rbac/cm.do?m=c", data, function(data) {
            	   var upfileId = jQuery('#fileUpload')[0].value;
            	   var ad_no;
            	   if(opt === 'm'){
            		   ad_no = $('#advertise_no').val();
            	   } else{
            		   var ad = data['added'];
            		   ad_no = ad['#ad.ssid_no'];
            	   }
            		      var parData = {file_name:upfileId,advertise_no:ad_no};
            		      if(upfileId!=null && upfileId!=""){
            		        $.ajaxFileUpload({  
            	                   url:'/cmwifi/rbac/impFile.do?method=impFile',             //需要链接到服务器地址  
            	                   secureuri:false,  
            	                   fileElementId:'fileUpload',                   //文件选择框的id属性  
            	                   dataType: 'json',                             //服务器返回的格式，可以是json  
            	                   data:parData,  
            	                   success: function (data, status) {
            	                       $.ligerDialog.success('保存成功', '操作完成',closeDialog);
            	                   },  
            	                   error: function (data, status, e){ 
            	                       $.ligerDialog.error(e,'上传失败');  
            	                   }  
            	                 });
            		      }else{
            		    	  $.ligerDialog.success('保存成功', '操作完成',closeDialog);
            		      }
               });
           }
 
           //重新加载页面 ，在保存时调用
            function closeDialog() {
                if (opt === 'm') {
                     parent.modifyDialog.hide();
                }else{
                     parent.addDialog.hide();
                }
                parent.toQuery();
              }
       </script>
       <style type="text/css">
           .center-height{height: 100%;}
       </style>
   </head>
    <body style="padding:0;">
       <div id="pageloading"></div>
       <div id="layout_main">
           <div position="center" id="a_center"  title="广告基本信息" >
              <form name="basic-form" method="post" id="basic_form" class="liger-form" data-validate="{}">
                <div class="line-div">
                 <div class="label-div"><label>商户名称</label></div>
                <div class="in-div"><input type="text" id="shop_nameS" ltype="text" class="readonly s-text txt-def" readonly ="readonly" /></div>
                <input type="hidden" id="shop_no" />
                 <div class="label-div"><label>广告编号</label></div>
                <div class="in-div"><input type="text" ltype="text" class="readonly s-text txt-def" id="advertise_no" readonly ="readonly" value="系统自动生成"/></div>
               </div>
                <div class="line-div">
                 <div class="label-div"><label>WIFI名称(ssid)</label></div>
                <div class="in-div"><input type="text" id="wifi_ssidS"  ltype="combobox" class="required s-text txt-def"/></div>
                 <input type="hidden" id="wifi_no" />
                <div class="label-div"><label>广告名称</label></div>
                <div class="in-div"><input type="text" ltype="text" class="required s-text txt-def" id="advertise_nameS" /></div>
      
               </div>
               <div class="line-div">
                <div class="label-div"><label>广告有效期</label></div>
                <div class="in-div"><input type="text"  ltype="date" class="required s-text txt-def" id="advertise_dateS" /></div>
                <div class="label-div"><label>广告级别</label></div>
                <div class="in-div"><input type="text" ltype="combobox" class="required s-text txt-def"  id="advertise_levelTxtS" /></div>
                <input type="hidden" id="advertise_level" />
                </div>
               <div class="line-div">
                <div class="label-div"><label>是否有效</label></div>
                <div class="in-div"><input type="text" id="advertise_useableTxtS" ltype="combobox" class="required s-text txt-def"  /></div>
                <input type="hidden" id="advertise_useable" />
                <div class="label-div"><label>广告内容</label></div>
                  <input type="file" name="fileUpload" id="fileUpload"/>
                </div>
                <div class="line-div">
                       <div class="label-div"><label>创建人</label></div>
                       <div class="in-div"><input type="text" id="createUserS" ltype="text" class="readonly diff" readonly="readonly" /></div>
                       <div class="label-div"><label>创建时间</label></div>
                       <div class="in-div"><input type="text" id="createDateS" ltype="text" class="readonly diff" readonly="readonly" /></div>
                       <input type="hidden" id="createUserCodeS" />
                       <input type="hidden" id="WriteUserCodeS" />
                       <input type="hidden" id="WriteDateS" />
                 </div>
            </form>
              <div class="line-div" style="margin-top:20px;padding-left:40%;">
                <input type="button" value="保存" onclick="toAdd();" />
                <input type="button" value="清空" onclick="toReset('#a_center input,select,textarea,date')" />
              </div>
       </div>
   </body>
</html>